package com.pactera.madp.cp.common.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * @Author:冯超云
 * @Date:2021/2/8 9:46
 * @Desc:分页工具类
 **/
public class PageUtil {
    /**
     * 内存分页
     *
     * @param list
     * @param current
     * @param size
     * @param <T>
     * @return
     */
    public static  <T> Page<T> split(List<T> list, Integer current, Integer size) {
        Page voPage = new Page();
        voPage.setTotal(list.size());
        voPage.setSize(size);
        voPage.setCurrent(current);
        int start = (current - 1) * size;
        int end = (start + size) > list.size() ? list.size() : start + size;
        if (start < list.size()) {
            voPage.setRecords(list.subList(start, end));
        }
        return voPage;
    }

    /**
     * 内存分页
     *
     * @param list
     * @param current
     * @param size
     * @param <T>
     * @return
     */
    public static  <T> Page<T> split(List<T> list, Integer current, Integer size, Long total) {
        Page voPage = new Page();
        voPage.setTotal(total);
        voPage.setSize(size);
        voPage.setCurrent(current);
        voPage.setRecords(list);
        return voPage;
    }

}
